package com.weme.live.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.alibaba.android.arouter.facade.annotation.Route
import com.bumptech.glide.Glide
import com.example.resources.R
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.weme.common.arouter.ARouterHelper
import com.weme.common.arouter.ARouterHelper.KEY_LIVE_TIME
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.util.*
import com.weme.common.util.LiveConfig.NET_WORK_LIVE_EVALUATE
import com.weme.live.databinding.ActivityLiveEvaluateBinding
import com.weme.live.view.OnRatingChangedListener
import com.weme.live.viewmode.LiveEvaluateViewMode
import dev.kratos.api.im_server.v1.CallEvaluateRequest
import dev.kratos.api.user_server.v1.Users
import me.jessyan.autosize.internal.CustomAdapt

@Route(path = ARouterPaths.LIVE_EVALVATE_ACTIVITY)
class LiveEvaluateActivity : BaseMvvmActivity<ActivityLiveEvaluateBinding, LiveEvaluateViewMode>(), KeyboardVisibilityListenerCallback, CustomAdapt {
    val maxLength = 140
    var star = 4
    lateinit var keyboardVisibilityListener: KeyboardVisibilityListener
    var channelName = ""
    var isHttp = false
    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).titleBar(mBinding.customTopNavigationBar).keyboardEnable(true).init()
        isHttp = false
        intent.extras?.apply {
            channelName = this.getString(ARouterHelper.KEY_CHANNEL_NAME).toString()
            val liveTime = this.getLong(KEY_LIVE_TIME)
            val hours = liveTime / 3600
            val minutes = (liveTime % 3600) / 60
            val remainingSeconds = liveTime % 60
            val formattedTime = String.format("%02d:%02d:%02d", hours, minutes, remainingSeconds)
            mBinding.tvTime.text = String.format(formattedTime)
            val user: Users = this.getSerializable(ARouterHelper.KEY_ANCHOR_BEAN) as Users
            mBinding.imHead.loadImage(user.headPic)
            mBinding.tvName.text = getString(com.example.resources.R.string.user_info_format, user.nickname, user.age)
            val country = getCountryBean(user.countryCode)
            country?.let {
                mBinding.tvCity.text = it.countryName
                Glide.with(this@LiveEvaluateActivity).load(it.countryFlag).into(mBinding.imFlag)
            }

        }
        keyboardVisibilityListener = KeyboardVisibilityListener(this)
        keyboardVisibilityListener.addVisibilityListener(this)
        mBinding.rsView.setListener(object : OnRatingChangedListener {
            override fun onRatingChanged(rating: Int) {
                when (rating) {
                    0 -> {
                        mBinding.tvLoved.text = getString(com.example.resources.R.string.live_evaluate_very_poor)
                        star = 0
                    }
                    1 -> {
                        mBinding.tvLoved.text = getString(com.example.resources.R.string.live_evaluate_poor)
                        star = 1
                    }
                    2 -> {
                        mBinding.tvLoved.text = getString(com.example.resources.R.string.live_evaluate_good)
                        star = 2
                    }
                    3 -> {
                        mBinding.tvLoved.text = getString(com.example.resources.R.string.live_evaluate_great)
                        star = 3
                    }
                    4, 5 -> {
                        mBinding.tvLoved.text = getString(com.example.resources.R.string.live_evaluate_loved)
                        star = 5
                    }
                }
            }
        })
        mBinding.scvSubmit.setOnFastClickListener {
            viewModel.putCallEvaluate(channelName, mBinding.edEvaluate.text.toString(), star)
            isHttp = true
        }

        mBinding.edEvaluate.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { // 不需要实现
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { // 不需要实现
            }

            override fun afterTextChanged(s: Editable?) {
                s?.let {
                    // 如果超过最大长度，截取字符串并显示提示
                    if (it.length > maxLength) {
                        val selectionStart = mBinding.edEvaluate.selectionStart
                        val selectionEnd = mBinding.edEvaluate.selectionEnd
                        it.replace(maxLength, it.length, "")
                        mBinding.edEvaluate.setSelection(selectionStart.coerceAtMost(maxLength), selectionEnd.coerceAtMost(maxLength))
                        showToast(resources.getString(com.example.resources.R.string.live_evaluate_ed_max))
                    }
                }
            }
        })
     LiveEventBus.get<Any>(NET_WORK_LIVE_EVALUATE).observe(this) { data ->
            if (data is CallEvaluateRequest) {
                if(isHttp){
                    showToast(resources.getString(R.string.live_evaluate_ed_success))
                    finish()
                }

            }
        }

    }

    override fun onDestroy() {
        viewModel.putCallEvaluate(channelName, mBinding.edEvaluate.text.toString(), star)
        super.onDestroy()
        keyboardVisibilityListener.removeVisibilityListener(this)
    }
    override fun isBaseOnWidth(): Boolean {
        return false
    }

    override fun getSizeInDp(): Float {
        return 800.0F
    }
    //
    override fun getViewModelClass(): Class<LiveEvaluateViewMode> = LiveEvaluateViewMode::class.java


    override fun onKeyboardVisibilityChanged(isVisible: Boolean, keyboardHeight: Int) {

    }


}